﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.Model;
using dhSystemBLL.basicData;

namespace dhsystem.basicForm
{
    public partial class FrmitemlistAdd : Form
    {
        itemListBLL item = new itemListBLL();
        itemListModel itemRow = new itemListModel();
        moduleListBLL mmoduleList = new moduleListBLL();
        // FrmItemlist itemList = new FrmItemlist();
        BindingSource bindingSourceitemAdd;

        string state;
        string count;
        string moduleNum;
        //绑定数据源
        public FrmitemlistAdd(BindingSource bindingSourceitemList, string _state, string _count, string _moduleNum)
        {
            InitializeComponent();
            state = _state;
            count = _count;
            bindingSourceitemAdd = bindingSourceitemList;
            moduleNum = _moduleNum;
        }
        //绑定数据源
        public FrmitemlistAdd(BindingSource bindingSourceitemList, string _state, string _count)
        {
            InitializeComponent();
            state = _state;
            count = _count;
            bindingSourceitemAdd = bindingSourceitemList;
           
        }
        //绑定数据源
        private void FrmitemlistAdd_Load(object sender, EventArgs e)
        {
            comboBoxmoduleNum.Focus();
            bindingSourcemoduleList.DataSource = mmoduleList.getmoduleListAll();
            comboBoxmoduleNum.DataSource = bindingSourcemoduleList;
            comboBoxmoduleNum.DisplayMember = "moduleName";
            comboBoxmoduleNum.ValueMember = "moduleNum";
           if (state == "编辑")
           {
                comboBoxmoduleNum.DataBindings.Add("text", bindingSourceitemAdd, "moduleNum");
                textitemNum.DataBindings.Add("text", bindingSourceitemAdd, "itemNum");
                textitemName.DataBindings.Add("text", bindingSourceitemAdd, "itemName");
                textitemDispC.DataBindings.Add("text", bindingSourceitemAdd, "itemDispC");
                textitemDispE.DataBindings.Add("text", bindingSourceitemAdd, "itemDispE");
                textitemDispJ.DataBindings.Add("text", bindingSourceitemAdd, "itemDispJ");
                textitemDispIndex.DataBindings.Add("text", bindingSourceitemAdd, "itemDispIndex");
                textitemWidth.DataBindings.Add("text", bindingSourceitemAdd, "itemWidth");
                checkBoxitemDispNo.DataBindings.Add("checked", bindingSourceitemAdd, "itemDispNo");
                textitemDataType.DataBindings.Add("text", bindingSourceitemAdd, "itemDataType");
                textUpdateUser.DataBindings.Add("text", bindingSourceitemAdd, "updateUser");
                textCreateUser.DataBindings.Add("text", bindingSourceitemAdd, "createUser");
                textmark.DataBindings.Add("text", bindingSourceitemAdd, "mark");
                comboBoxmoduleNum.SelectedValue = moduleNum;
            }
            if (state == "新增")
            {
                textitemNum.Text = count;
            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增模块表格！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        itemRow.moduleNum = comboBoxmoduleNum.SelectedValue.ToString();
                        itemRow.itemNum = textitemNum.Text;
                        itemRow.itemName = textitemName.Text;
                        itemRow.itemDispC = textitemDispC.Text;
                        itemRow.itemDispE = textitemDispE.Text;
                        itemRow.itemDispJ = textitemDispJ.Text;
                        itemRow.itemDispIndex = Convert.ToInt32(textitemDispIndex.Text);
                        itemRow.itemWidth = Convert.ToInt32(textitemWidth.Text);
                        itemRow.itemDispNo = checkBoxitemDispNo.Checked;
                        itemRow.itemDataType = textitemDataType.Text;
                        itemRow.createDate = DateTime.Now;
                        itemRow.updateDate = DateTime.Now;
                        itemRow.createUser = textCreateUser.Text;
                        itemRow.updateUser = textUpdateUser.Text;
                        itemRow.mark = textmark.Text;
                        item.itemListInsert(itemRow);
                        bindingSourceitemAdd.DataSource = item.getitemListAll();
                        MessageBox.Show("新增模块列表成功");



                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改模块表格！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        itemRow.moduleNum = comboBoxmoduleNum.SelectedValue.ToString();
                        itemRow.itemNum = textitemNum.Text;
                        itemRow.itemName = textitemName.Text;
                        itemRow.itemDispC = textitemDispC.Text;
                        itemRow.itemDispE = textitemDispE.Text;
                        itemRow.itemDispJ = textitemDispJ.Text;
                        itemRow.itemDispIndex = Convert.ToInt32(textitemDispIndex.Text);
                        itemRow.itemWidth = Convert.ToInt32(textitemWidth.Text);
                        itemRow.itemDispNo = checkBoxitemDispNo.Checked;
                        itemRow.itemDataType = textitemDataType.Text;
                        itemRow.createDate = DateTime.Now;
                        itemRow.updateDate = DateTime.Now;
                        itemRow.createUser = textCreateUser.Text;
                        itemRow.updateUser = textUpdateUser.Text;
                        itemRow.mark = textmark.Text;
                        item.itemListUpdate(itemRow);
                        bindingSourceitemAdd.DataSource = item.getitemListAll();
                        MessageBox.Show("修改模块列表成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
